<template>
  <van-cell-group>
    <van-field
      v-model="original"
      type="password"
      clearable
      label="旧密码"
      placeholder="请输入旧密码"
      required
    ></van-field>

    <van-field
      v-model="change1"
      type="password"
      clearable
      label="新密码"
      placeholder="请输入新密码"
      required
    ></van-field>

    <van-field
      v-model="change2"
      type="password"
      clearable
      label="新密码"
      placeholder="请再次输入新密码"
      required
    ></van-field>
  </van-cell-group>
</template>

<script>
  import {CellGroup, Field, Toast} from 'vant'

  export default {
    name: "Password",
    components: {
      [CellGroup.name]: CellGroup,
      [Field.name]: Field,
      [Toast.name]: Toast,
    },
    data() {
      return {
        original: "",
        change1: "",
        change2: "",
      }
    },
    methods: {
      setMsg() {
        let _this = this;
        if (!_this.original || !_this.change1 || !_this.change2) {
          Toast.fail('请输入全部信息！');
          _this.returnFlag(false);
          return;
        }
        let token = _this.getCookie("token");
        if (_this.change1 !== _this.change2) {
          Toast.fail('确认输入的密码不一致！请重新输入。');
          _this.returnFlag(false);
          return;
        }
        let data = _this.$qs.stringify({
          'original': _this.original,
          'change1': _this.change1,
          'change2': _this.change2
        });
        _this.$http.post(_this.apiUrl("user/users/changePwd"), data, {headers: {'token': token}}).then((res) => {
          // console.log(res.data);
          let code = res.data.statusCode;
          if (code === 200) {
            Toast.success('修改成功！');
            _this.returnFlag(true);
          }
          else{
            _this.returnFlag(false);
            let reason = res.data.reason;
            Toast.fail('修改失败！' + reason);
          }
        });
      },
      returnFlag(flag) {
        let _this = this;
        if (flag) {
          _this.$emit("giveFlag", _this.randomWord(true, 10, 20));
        }
        else {
          _this.$emit("giveFlag", "null");
        }
      },
    },
  }
</script>

<style scoped>
</style>
